package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Commodity;
import cn.wolfcode.trip.base.domain.CommodityForm;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.CommodityFormMapper;
import cn.wolfcode.trip.base.mapper.CommodityMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ICommodityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommodityServiceImpl implements ICommodityService {
    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private CommodityFormMapper formMapper;

    public PageInfo<?> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Commodity> list = commodityMapper.queryForList(qo);
        return new PageInfo<>(list);
    }

    public void saveOrUpdate(Commodity commodity) {
        if (commodity.getId() == null){
            commodity.setSalesVolume(0);
            commodityMapper.insert(commodity);
        }else {
            commodityMapper.updateByPrimaryKey(commodity);
        }
    }

    public void deleteById(Long id) {
        commodityMapper.deleteByPrimaryKey(id);
    }

    public List<Commodity> getForms(Long userId) {
        return commodityMapper.getFormsByUserId(userId);
    }
}
